package com.icez.base_compose_framework.ui.home

import androidx.compose.material3.Text
import androidx.compose.material3.adaptive.WindowAdaptiveInfo
import androidx.compose.material3.adaptive.layout.HingePolicy
import androidx.compose.material3.adaptive.layout.PaneScaffoldDirective
import androidx.compose.material3.adaptive.navigation.rememberSupportingPaneScaffoldNavigator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.max
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.window.core.layout.WindowSizeClass
import androidx.window.core.layout.WindowWidthSizeClass
import com.icez.base_compose_framework.util.BWindowSizeClass
import com.icez.base_compose_framework.util.BWindowSizeClassUtil
import com.icez.domain.model.EpisodeInfo

@Composable
fun HomeScreen(viewModel:HomeScreenViewModel = hiltViewModel()) {
    val homeScreenState by viewModel.state.collectAsStateWithLifecycle()
    when(val state = homeScreenState){
        is HomeScreenState.Loading ->{
            Text(text = "加载中...")
        }
        is HomeScreenState.Error ->{
            Text(text = "加载失败")
        }
        is HomeScreenState.Ready->{
            Text(text = "准备加载数据显示UI")
        }
    }
}

@Composable
private fun HomeScreenReady(uiState:HomeScreenState.Ready, windowSizeClass:WindowSizeClass, navigateToPlayer:(EpisodeInfo)->Unit, viewModel:HomeScreenViewModel = hiltViewModel()){
    val navigator = rememberSupportingPaneScaffoldNavigator<String>(scaffoldDirective = calculateScaffoldDirective())
}


fun calculateScaffoldDirective(windowAdaptiveInfo: WindowAdaptiveInfo,verticalHingePolicy: HingePolicy = HingePolicy.AvoidSeparating): PaneScaffoldDirective {
    val maxHorizontalPartitions:Int
    val verticalSpacerSize: Dp
    if(BWindowSizeClassUtil.isCompact(windowAdaptiveInfo.windowSizeClass)){
        maxHorizontalPartitions = 1
        verticalSpacerSize = 0.dp
    }else{
        when(windowAdaptiveInfo.windowSizeClass.windowWidthSizeClass){
            WindowWidthSizeClass.COMPACT -> {
                maxHorizontalPartitions = 1
                verticalSpacerSize = 0.dp
            }
            WindowWidthSizeClass.MEDIUM -> {
                maxHorizontalPartitions = 1
                verticalSpacerSize = 0.dp
            }
            else -> {
                maxHorizontalPartitions = 2
                verticalSpacerSize = 24.dp
            }
        }
    }

    val maxVerticalPartitions:Int
    val horizontalSpacerSize: Dp

    if (windowAdaptiveInfo.windowPosture.isTabletop){
        maxVerticalPartitions = 2
        horizontalSpacerSize = 24.dp
    }else{
        maxVerticalPartitions = 1
        horizontalSpacerSize = 0.dp
    }

    val defaultPanePreferredWidth = 360.dp

    return PaneScaffoldDirective(
        maxHorizontalPartitions,
        verticalSpacerSize,
        maxVerticalPartitions,
        horizontalSpacerSize,
        defaultPanePreferredWidth,
        get
    )
}
